import pymysql
  
conn = pymysql.connect(
    host = '127.0.0.1',
    port = 3306,
    user = 'root',
    passwd = '123456',
    db = 'feizhi',
    charset = 'utf8'
)

cursor = conn.cursor()

#插入
#insert_dep = 'insert into departments values(%s, %s)'
#cursor.executemany(insert_dep,[(1,'人事部')])    #增加一条语句
#deps = [(2,'财务部'),(3,'运维部'),(4,'开发部'),(5,'测试部'),(6,'市场部')]
#cursor.executemany(insert_dep,deps)

#查询
select1 = 'select * from departments'
cursor.execute(select1)
print(cursor.fetchone())
print('*' * 20)
print(cursor.fetchmany())
print('*' * 20)
print(cursor.fetchall())

select2 = 'select * from departments order by dep_id'
cursor.execute(select2)
cursor.scroll(2,mode='relative')    #以相对方式向下移动2行记录
print(cursor.fetchone())
print('*' * 20)
cursor.scroll(0,mode='absolute')    #以绝对方式移动到第1行记录
print(cursor.fetchone())

#修改
update1 = 'update departments set dep_name=%s where dep_name=%s'
cursor.execute(update1,('人力资源部','人事部'))

#删除
delete1 = 'delete from departments where dep_name=%s'
cursor.execute(delete1,('市场部',))

conn.commit()
cursor.close()
conn.close()

